package com.company.project.service.impl;

import com.company.project.core.Page;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.dao.CollegeDao;
import com.company.project.model.College;
import com.company.project.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description:
 *
 * @author:
 * Date: 2020-04-03
 * Time: 16:04
 */
@Service
public class CollegeServiceImpl implements CollegeService {
    @Autowired
    CollegeDao collegeDao;

    /**
     * 分页查询所有院系
     *
     * @param pageInfo 分页参数
     * @return
     */
    @Override
    public Result queryCollegePage(Page pageInfo) {
        Integer count = collegeDao.countByCondition();
        pageInfo.setAllRow(count);
        List<College> collegeList  = collegeDao.selectByCondition( pageInfo.getStart(), pageInfo.getSize());
        return ResultGenerator.genSuccessResult(collegeList);
    }

    @Override
    public Result queryCollege() {
        List<College> colleges = collegeDao.selectAll();
        return ResultGenerator.genSuccessResult(colleges);
    }

    @Override
    public Result getCollege(Integer id) {
        College college = collegeDao.selectByPrimaryKey(id);
        return ResultGenerator.genSuccessResult(college);
    }

    @Override
    public Result saveCollege(College college) {
        if (college.getId() == null){
            collegeDao.insert(college);
        }else{
            collegeDao.updateByPrimaryKey(college);
        }
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result deleteCollege(Integer id) {
        int deleteByPrimaryKey = collegeDao.deleteByPrimaryKey(id);
        if(deleteByPrimaryKey > 0){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult();
        }
    }

    @Override
    public Result getCollegeByName(String collegeName) {
        College college = collegeDao.getCollegeByName(collegeName);
        return ResultGenerator.genSuccessResult(college);
    }
}
